package com.botarena.server.servlets.http;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.botarena.server.services.AdminService;

public class ResetDatabaseServlet extends HttpServlet {
	
	private static final long serialVersionUID = -110426020592637852L;
	
	private static final Logger log = Logger.getLogger(ResetDatabaseServlet.class.getName());
	private static final AdminService admin = new AdminService();

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setContentType("text/html");
		
		try {
			log.info("Performing database reset");
			
			int deleted = admin.resetDatastore();
			
			response.getOutputStream().write(Integer.toString(deleted).getBytes());
			
			admin.resetAndCreateExampleContent();
			
			//TODO: other things
		} catch (Exception e) {
			e.printStackTrace();
			String error = "Cannot reset database: " + e.getMessage();
			response.getOutputStream().write(error.getBytes());
		} finally {
			response.getOutputStream().close();
		}
	}
}
